/**
 * File Name   : ProductTypeAction.java
 * Description : 
 */
package com.hh.admin.producttype.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.common.service.CommonService;
import com.hh.admin.producttype.form.ParamProductTypeBean;
import com.hh.admin.producttype.form.ProductTypeForm;
import com.hh.admin.producttype.form.ResultProductTypeBean;
import com.hh.admin.producttype.service.ProductTypeService;
import com.hh.common.consts.AdminConst;

/**
 * 产品类型管理 Action
 * @author 冯昌硕
 * @create 2013-1-1 下午1:24:15
 */
public class ProductTypeAction extends MappingDispatchAction {

    private static final Logger log = Logger.getLogger(ProductTypeAction.class);
    
    private ProductTypeService producttypeService;
    
    private CommonService commonService;
    
    /**
     * 获取产品类型列表
     * @create 2013-1-1 下午1:30:11
     * @modify 2013-01-20 15:38 分页查询改由CommonSerivice实现
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTypeForm ptForm = (ProductTypeForm) form;
        
        // 查询当前页要显示的记录
        ParamProductTypeBean paramBean = new ParamProductTypeBean();
        List<ResultProductTypeBean> producttypeLst = 
                commonService.selRecordList(ptForm, "producttype.selProductTypeList", paramBean);
        request.setAttribute("producttypeLst", producttypeLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "producttype.selProductTypeCount");
        ptForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / ptForm.getPageSize();
        if(totleCnt % ptForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        ptForm.setTotlePage(pageCnt);
        log.debug("查询产品类型记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 删除单条产品类型记录
     * @create 2012-12-26 下午11:47:13
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTypeForm ptForm = (ProductTypeForm) form;
        
        int id = ptForm.getId();
        log.debug("产品类型删除，要删除的产品类型ID=[" + id + "]");
        int delCnt = producttypeService.deleteOneProducttype(id);
        log.debug("产品类型[" + id + "]删除，影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 批量删除
     * @create 2013-1-1 下午3:27:10
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTypeForm ptForm = (ProductTypeForm) form;
        Integer[] ids = ptForm.getIds();
        
        int delCnt = producttypeService.deleteManyProducttype(ids);
        log.debug("批量产品类型删除，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("view");
    }
    
    /**
     * 打开产品类型详情页进行内容编辑
     * @create 2013-1-1 下午3:37:05
     */
    public ActionForward detail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTypeForm ptForm = (ProductTypeForm) form;
        int id = ptForm.getId();
        ResultProductTypeBean result = producttypeService.getProductTypeDetail(id);
        
        request.setAttribute("producttypeDetail", result);
        
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的产品类型内容更新
     * @create 2013-1-1 下午3:59:20
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTypeForm ptForm = (ProductTypeForm) form;
        int updCnt = producttypeService.updateProductType(ptForm);
        log.debug("修改产品类型[" + ptForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        
        // 更新后刷新框架页面左侧菜单页面
        request.setAttribute("refreshLeft", true);
        
        return mapping.findForward("success");
    }
    
    /**
     * 添加产品类型记录
     * @create 2012-12-27 下午8:44:14
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductTypeForm ptForm = (ProductTypeForm) form;
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        ptForm.setCreateUser(loginUserInfo.getLoginName());
        
        int newId = producttypeService.insertProducttype(ptForm);
        log.debug("新增产品类型ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }

    public void setProducttypeService(ProductTypeService producttypeService) {
        this.producttypeService = producttypeService;
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }
}
